#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void InitBoard(char board[3][3], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}

//void DisplayBoard(char board[3][3], int r, int c)
//{
//	for (int i = 0; i < r; i++)
//	{
//		for (int j = 0; j < c; j++)
//		{
//			printf(" %c ", board[i][j]);
//		}
//		printf("\n");
//	}
//}

void DisplayBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		//先打印数据
		printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]);

		//再打印分隔行
		if (i < row - 1)
		{
			printf("---|---|---\n");
		}
	}
}

void PlayerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("玩家下棋\n");

	while (1)
	{
		printf("请输入下棋坐标:");
		scanf("%d %d", &x, &y);
		if (x <= row && x >= 1 && y <= col && y >= 1)
		{
			if (board[x - 1][y - 1] == ' ')//为空可以落子
			{   //[x-1][y-1]可以让输入的数字变为数组下标
				board[x - 1][y - 1] = 'o';
				break;
			}
			else
			{
				printf("该坐标已占用，请重新输入\n");
			}
		}
		else
		{
			printf("坐标非法，请重新输入\n");
		}
	}
}

//判断连续情况
int find(char board[ROW][COL], int row, int col)
{
	int i = 0;
	//行判断
	for (i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][2] == ' ' && board[i][0] != ' ')
		{
			board[i][2] = 'x';
			return 1;
		}
		if (board[i][0] == board[i][2] && board[i][1] == ' ' && board[i][0] != ' ')
		{
			board[i][1] = 'x';
			return 1;
		}
		if (board[i][1] == board[i][2] && board[i][0] == ' ' && board[i][1] != ' ')
		{
			board[i][0] = 'x';
			return 1;
		}
	}
	//列判断
	for (i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[2][i] == ' ' && board[0][i] != ' ')
		{
			board[2][i] = 'x';
			return 1;
		}
		if (board[0][i] == board[2][i] && board[1][i] == ' ' && board[0][i] != ' ')
		{
			board[1][i] = 'x';
			return 1;
		}
		if (board[1][i] == board[2][i] && board[0][i] == ' ' && board[1][i] != ' ')
		{
			board[0][i] = 'x';
			return 1;
		}
	}
	//对角线判断
	if (board[1][1] == board[0][0] && board[2][2] == ' ' && board[1][1] != ' ')
	{
		board[2][2] = 'x';
		return 1;
	}
	if (board[0][0] == board[2][2] && board[1][1] == ' ' && board[0][0] != ' ')
	{
		board[1][1] = 'x';
		return 1;
	}
	if (board[1][1] == board[2][2] && board[0][0] == ' ' && board[1][1] != ' ')
	{
		board[0][0] = 'x';
		return 1;
	}
	//斜对角线判断
	if (board[0][2] == board[1][1] && board[2][0] == ' ' && board[0][2] != ' ')
	{
		board[2][0] = 'x';
		return 1;
	}
	if (board[1][1] == board[2][0] && board[0][2] == ' ' && board[1][1] != ' ')
	{
		board[0][2] = 'x';
		return 1;
	}
	if (board[0][2] == board[2][0] && board[1][1] == ' ' && board[0][2] != ' ')
	{
		board[1][1] = 'x';
		return 1;
	}
	return 0;
}

//电脑随机下棋
void ComputerMove(char board[ROW][COL], int row, int col)
{
	printf("电脑下棋\n");
	int x = 0;
	int y = 0;

	//判断中间，抢中心点
	if (board[1][1] == ' ')
	{
		board[1][1] = 'x';
	}
	else if (find(board, row, col)) //判断是否复合拦截条件，复合进行拦截。
	{
		;
	}
	else
	{
		while (1)
		{
			x = rand() % row;
			y = rand() % col;
			if (board[x][y] == ' ')
			{
				board[x][y] = 'x';
				break;
			}
		}
	}
}

//判断棋盘是否满了
int IsFull(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
			{
				return 0;//有位置返回 0
			}
		}
	}
	return 1;//棋盘满了返回 1
}

//玩家赢-----w
//电脑赢-----d
//平局-------p    满了返回1，不满返回0
//游戏继续---j
char IsWin(char board[ROW][COL], int row, int col)
{
	//行判断
	int i = 0;
	for (i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
		{
			return board[i][0]; //三个都相等的话，然后其中一个即可
		}
	}
	//列判断
	for (i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
		{
			return board[1][i];
		}
	}
	//对角线判断
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
	{
		return board[1][1];
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
	{
		return board[1][1];
	}

	//没有人赢，输出平局
	if (IsFull(board, row, col) == 1)
	{
		return 'p';
	}

	//游戏继续
	return 'j';
}

//延迟打印
void delay(int milliseconds)
{
	int i, j;
	for (i = 0; i < milliseconds; i++)
	{
		for (j = 0; j < 10000; j++)
		{
			// 空循环，用于消耗一定的时间
		}
	}
}